package com.lianniu.mgr.baseService.file.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.buyerManage.action.BuyerManageRestController;
import com.lianniu.mgr.util.FileUploadUtil;
import com.lianniu.mgr.util.StringUtil;

@Controller
@RequestMapping("/rest/fileUpload")
public class FileUploadRestController {
  private static Logger logger = LoggerFactory.getLogger(BuyerManageRestController.class);
  @Autowired
  FileUploadUtil fileUpload;
  
  @RequestMapping(value = {"/uploadPic"}, method = {  RequestMethod.POST })
  public @ResponseBody BaseResult uploadPic(HttpServletRequest request) throws Exception {
      logger.info("ProcessController add begin.");
      MultipartHttpServletRequest re = (MultipartHttpServletRequest) request;
      MultipartFile file = re.getFile(request.getParameter("id"));
      String path= request.getParameter("filepath");
      if(StringUtil.isEmpty(path)){
        path = "/common/";
      }
      if(file != null){
        String uploadedPath = fileUpload.getPath(file, request, "/" + path + "/");
        return BaseResult.success(uploadedPath);
      }
      return BaseResult.success();
  }
}
